//
//  IndoorMapPathView.m
//  WisdomMallAPP
//
//  Created by apple on 14-4-15.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "IndoorMapPathView.h"

#import "Constant.h"
#import "AStarItem.h"



@interface IndoorMapPathView ()
{
    CGFloat offset_y;
    NSMutableArray *newPaths;
}
@property(nonatomic,strong)UILabel * navLabel;
@end

@implementation IndoorMapPathView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        //iPhone 5
        offset_y = OFFSET_Y;
        
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    //抗锯齿
    CGContextSetAllowsAntialiasing(context, TRUE);
    CGContextSetShouldAntialias(context, true);
    
    //图片大小458*404
    //转化成可绘制坐标
//    CGFloat x_ratio = kDeviceWidth / 1362.0f;
//    
//    CGFloat h_ratio = 0.63;
//    if (IS_IPHONE_5) {
//        h_ratio = 0.52f;
////        offset_y += 46.0f;
//        offset_y = 131.0f;
//    }
//    float imageHeight = kDeviceWidth*803/1362;
//    h_ratio = imageHeight/kDeviceHeight;
//    CGFloat y_ratio = kDeviceHeight / 803.f * h_ratio;
//    offset_y = (kDeviceHeight - imageHeight)/2;
//    
//    x_ratio=1;
//    offset_y=1;
    if (newPaths) {
        NSMutableArray * noticeArray = [NSMutableArray array];
        
       for (int j = 0 ; j<newPaths.count; j++) {
           AStarItem * item = newPaths[j];
            if (j == 0){
                CGContextMoveToPoint(context, item.id_col, item.id_row );  // 开始坐标右边开始
//                [noticeArray addObject:@"出发"];
            }else{
                CGContextAddLineToPoint(context, item.id_col , item.id_row );
                
                if (j<newPaths.count-1) {
                    AStarItem * lastItem = newPaths[j-1];
                    AStarItem * nextItem = newPaths[j+1];
                    float a = sqrt((item.id_col-nextItem.id_col)*(item.id_col-nextItem.id_col)+(item.id_row-nextItem.id_row)*(item.id_row-nextItem.id_row));
                    float b = sqrt((lastItem.id_col-nextItem.id_col)*(lastItem.id_col-nextItem.id_col)+(lastItem.id_row-nextItem.id_row)*(lastItem.id_row-nextItem.id_row));
                    float c = sqrt((lastItem.id_col-item.id_col)*(lastItem.id_col-item.id_col)+(lastItem.id_row-item.id_row)*(lastItem.id_row-item.id_row));
                    float B=acos((a*a+c*c-b*b)/(2*a*c))*180/3.141592654;
                    if (B==180.f) {//直行
                        [noticeArray addObject:@"前方直行"];
                    }else if(B>=135.f&&B<180.f){//小转弯
                        if (abs(item.id_col-lastItem.id_col)>abs(item.id_row-lastItem.id_row)) {//水平移动
                            if (item.id_col<lastItem.id_col) {//向左水平移动
                                if(item.id_row>nextItem.id_row){
                                    [noticeArray addObject:@"前方往右前方直行"];
                                }else{
                                    [noticeArray addObject:@"前方往左前方直行"];
                                }
                            }else{//向右水平移动
                                if(item.id_row>nextItem.id_row){
                                    [noticeArray addObject:@"前方往左前方直行"];
                                }else{
                                    [noticeArray addObject:@"前方往右前方直行"];
                                }

                            }
                        }else{//垂直移动
                            
                            if (item.id_row<lastItem.id_row) {//向上垂直运动
                                if(item.id_col>nextItem.id_col){
                                    [noticeArray addObject:@"前方往左前方直行"];
                                }else{
                                    [noticeArray addObject:@"前方往右前方直行"];
                                }
                            }else{//向下垂直运动
                                if(item.id_col>nextItem.id_col){
                                    [noticeArray addObject:@"前方往右前方直行"];
                                }else{
                                    [noticeArray addObject:@"前方往左前方直行"];
                                }
                            }
                            
                        }
                        
                    }else if(B>0.f&&B<135.f){//转弯
                        if (abs(item.id_col-lastItem.id_col)>abs(item.id_row-lastItem.id_row)) {//水平移动
                            if (item.id_col<lastItem.id_col) {//向左水平移动
                                if(item.id_row>nextItem.id_row){
                                    [noticeArray addObject:@"前方右转"];
                                }else{
                                    [noticeArray addObject:@"前方左转"];
                                }
                            }else{//向右水平移动
                            if(item.id_row>nextItem.id_row){
                                [noticeArray addObject:@"前方左转"];
                            }else{
                                [noticeArray addObject:@"前方右转"];
                            }
                            }
                        }else{//垂直移动
                             if (item.id_row<lastItem.id_row) {//向上垂直运动
                            if(item.id_col>nextItem.id_col){
                                [noticeArray addObject:@"前方左转"];
                            }else{
                                [noticeArray addObject:@"前方右转"];
                            }
                             }else{//向下垂直运动
                                 if(item.id_col>nextItem.id_col){
                                     [noticeArray addObject:@"前方右转"];
                                 }else{
                                     [noticeArray addObject:@"前方左转"];
                                 }
                             }
                        }
                        
                    }
                }else{
                    [noticeArray addObject:@"到达目的地附近"];

                }

            }
           
           
        }
        
        if (newPaths) {
            self.navLabel.hidden = NO;
            self.navLabel.text = [noticeArray firstObject];
        }
        CGContextSetLineWidth(context, 2.0f);
        CGFloat lengths[] = {3, 2};
        CGContextSetLineDash(context, 0, lengths, 2);
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//线框颜色
        CGContextStrokePath(context);
    }
}

- (void)drawPathWithPoints:(NSMutableArray *const)paths
{
    newPaths = paths;
    [self setNeedsDisplay];
}

- (UILabel *)navLabel{
    if (!_navLabel) {
        _navLabel = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, 450, 320, 30), NO)];
        _navLabel.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
        _navLabel.textColor = [UIColor whiteColor];
        _navLabel.tag = 50;
        _navLabel.textAlignment = NSTextAlignmentCenter;
        [self.superview.superview.superview insertSubview:_navLabel atIndex:2];
    }
    return _navLabel;
}
@end
